<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Ajax extends Controller {
        
	public function action_count($number)
	{
		$this->request->response = ($number * $number);
	}

        public function action_nihssStart(){
                $c_id = $_POST['c_id'];
                $p_id = $_POST['p_id'];
                $consult = new Model_Consult();
                echo "เริ่มการตรวจเมื่อ <font color=red>".$consult->updateStartNIHSS($p_id, $c_id)."</font>";
        }

        public function action_nihssEnd(){
                $c_id = $_POST['c_id'];
                $p_id = $_POST['p_id'];
                $consult = new Model_Consult();
                echo "สิ้นสุดการตรวจเมื่อ <font color=red>".$consult->updateEndNIHSS($p_id, $c_id)."</font>";
                echo "<br>";

                echo "ใช้เวลาไป ".$consult->getTimeUseNIHSS($p_id, $c_id);
        }

        public function action_sendChat(){
                $c_id = $_POST['c_id'];
                $p_id = $_POST['p_id'];
                $chat_txt = isset($_POST['chat_txt'])?$_POST['chat_txt']:"";
                $from = $_POST['from'];
                if($from=="P"){
                    $color = "blue";
                }else{
                    $color = "red";
                }
                $text = "<font color=".$color."><b>$from: </b>$chat_txt</font> <font style=\"font-style: italic;font-size: 11px;color: green\"><i>(".date("Y-m-d H:i:s").")</i></font>";
                $consult_model = new Model_Consult();
                $consult_model->sendChat($p_id, $c_id, $text);
        }

        public function action_loadChat(){
                $c_id = $_POST['c_id'];
                $p_id = $_POST['p_id'];
                $consult_model = new Model_Consult();
                echo $consult_model->loadChat($p_id, $c_id);
        }

        public function action_endConsult(){
                $c_id = $_POST['c_id'];
                $p_id = $_POST['p_id'];
                $consult_model = new Model_Consult();
                $consult_model->endConsult($p_id, $c_id);
        }

        public function action_checkStatus(){
                $c_id = $_GET['c_id'];
                $p_id = $_GET['p_id'];
                
                $consult_model = new Model_Consult();
                
                if($consult_model->checkStatus($p_id,$c_id)=="Consulting")
                    echo "true";
                else
                    echo "false";
        }

        public function action_loadNIHSSData(){
                 $c_id = $_POST['c_id'];
                 $p_id = $_POST['p_id'];
                 $consult = new Model_Consult();
                 $data = $consult->loadNIHSSData($p_id, $c_id);
                 echo json_encode($data);
        }

        public function action_loadPatientData(){
                 $c_id = $_POST['c_id'];
                 $p_id = $_POST['p_id'];
                 $consult = new Model_Consult();
                 $nihss = $consult->loadNIHSSData($p_id, $c_id);
                 $strokeData = $consult->loadStrokeData($p_id, $c_id);
                 $strokePhyData = $consult->loadStrokePhy($p_id, $c_id);
                 $laboratoryData = $consult->loadLaboratoryData($p_id, $c_id);
                 $estimateData = $consult->loadEstimateData($p_id, $c_id);
                 $managementData = $consult->loadManagementData($p_id, $c_id);
                 $data = array_merge($nihss,$strokeData,$strokePhyData,$laboratoryData,$estimateData,$managementData);
                 echo json_encode($data);
        }

        public function action_loadManagementData(){
                $c_id = $_POST['c_id'];
                 $p_id = $_POST['p_id'];
                 $consult = new Model_Consult();
                 $managementData = $consult->loadManagementData($p_id, $c_id);
                 echo json_encode($managementData);
        }

        public function action_updateSuggestManagement(){
                 $c_id = $_POST['c_id'];
                 $p_id = $_POST['p_id'];
                 $consult = new Model_Consult();
                 $consult->updateSuggestionManagement($p_id, $c_id, $_POST);
        }

        public function action_checkWaitingPatient(){
            $consult_doctor_id = session::instance()->get('consult_doctor_id');
            $consult = new Model_Consult();
            if(isset($consult_doctor_id)) {
                $patients = $consult->getWaitingPatient($consult_doctor_id);
                if($patients!=null) {
                    foreach ($patients as $patient) {
                        echo "<tr><td height=20 align=left><a href='".URL::base()."doctor/consultpage?p_id=".$patient['p_id']."'>".$patient['p_fname']." ".$patient['p_lname']."</a></td></tr>";
                    }
                }else {
                    echo "<tr><td valign=top align=left>--</td></tr>";
                }
            }
        }

        public function action_saveNIHSSCData(){
                 $c_id = $_POST['c_id'];
                 $p_id = $_POST['p_id'];
                 $consult = new Model_Consult();
                 $consult->updateNIHSSCData($p_id, $c_id, $_POST);
        }
        
        public function action_saveEstimateCData(){
                 $c_id = $_POST['c_id'];
                 $p_id = $_POST['p_id'];
                 $consult = new Model_Consult();
                 $consult->updateEstimateCData($p_id, $c_id, $_POST);
        }

        public function action_ctscan(){
            if(isset($_POST['p_id'])){
                $p_id = $_POST['p_id'];
                $c_id = $_POST['c_id'];
            }

            $sql = "SELECT * FROM ct_scan where p_id=".$p_id." and c_id=".$c_id;
            $query = Database::instance()->query(Database::SELECT, $sql, FALSE)->as_array();

//            $sql2 = "SELECT comment_id FROM img_comment";
//            $query2 = Database::instance()->query(Database::SELECT, $sql2, FALSE)->as_array();
            
            if(count($query)!=0){
                 $ct_images = $query;
            }else{
                 $ct_images = null;
            }
            
//            if(count($query2)!=0)
//            {
//                $comment_id = count($query2);
//            }
//            else
//            {
                $comment_id = 0;
//            }

            $index = 1;
            if($ct_images!=null){
                foreach ($ct_images as $ct) {
                    $filename = "ct/".$ct['p_id']."/".$ct['year']."/".$ct['month']."/".$ct['filename'];
                    $ct_id = $ct['ct_id'];
                    list($img_width, $img_height, $type, $attr) = getimagesize($filename);
                    
//                    $filepath="..".URL::base().$filename;
                    $filepath="../".$filename;
                    
             /*       echo "<td><table style='padding:10px;background-color: #aaaaaa;border:solid black 1px' width='200'>
                        <tr><td>
                        ".html::image($filename,array('width'=>'150','height'=>'100'))."
                        </td>
                        <td>
                            
                            <a href='../$filename' target='_blank'>".html::image('assets/icon/search_48.png',array('border'=>'0','alt'=>'เปิด','width'=>'32','height'=>'32'))."</a>
                        </td>
                        </tr>
                        <tr><td colspan=2>".$ct["description"]."</td></tr>
                        </table></td>";*/
                    echo "<td><table style='padding:10px;background-color: #aaaaaa;border:solid black 1px' width='200'>
                        <tr><td>
                        ".html::image($filename,array('width'=>'150','height'=>'100'))."
                        </td>
                        <td>
                            <a href='".URL::base()."doctor/editct?path=".$filepath."&c_id=".$c_id."&p_id=".$p_id."&ct_id=".$ct_id."&img_width=".$img_width."&img_height=".$img_height."&comment_id=".$comment_id."' target='_blank'>".html::image('assets/icon/search_48.png',array('border'=>'0','alt'=>'เปิด','width'=>'32','height'=>'32'))."</a>
                        </td>
                        </tr>
                        <tr><td colspan=2>".$ct["description"]."</td></tr>
                        </table></td>";
                    $index++;
            }
          }
        }
        
        public function action_sendCoordinate(){
                //$comment_id = $_POST['comment_id'];
                $c_id = $_POST['c_id'];
                $p_id = $_POST['p_id'];
                $ct_id = $_POST['ct_id'];
                $data = $_POST['data'];
                                
                $comments = json_decode($data);
                $ctscan = new Model_CtScan();
                $ctscan->clearComment($c_id, $p_id, $ct_id);
                
                if($data==''){
                    return;
                }
                                               
                foreach ($comments as $comment) { 
                    
                    echo $comment->x.',';
                    $ctscan->insertImgComment(                           
                            $c_id,
                            $p_id,
                            $ct_id,
                            $comment->id,
                            $comment->x, 
                            $comment->y,
                            $comment->w,
                            $comment->h,
                            $comment->comment);                                                                         
                }
                //$co_x = $_POST['co_x'];
                //$co_y = $_POST['co_y'];
                //$co_w = $_POST['co_w'];
                //$co_h = $_POST['co_h'];
                //$comment = $_POST['comment'];
                //$status = $_POST['status'];
                
                           
                
        }
        
        public function action_getCoordinate(){
            $c_id = $_POST['c_id'];
            $p_id = $_POST['p_id'];
            $ct_id = $_POST['ct_id'];
            $ctscan = new Model_CtScan();
            $comment_data = $ctscan->getImgComment($c_id,$p_id,$ct_id);
            echo json_encode($comment_data);
        }
      
        
} // End Welcome
